package me.zhengjie.wcs;

import cn.hutool.core.util.ObjectUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.dto.sqlServer.DeviceStatus;
import me.zhengjie.dto.sqlServer.TaskResult;
import me.zhengjie.repository.DeviceStatusRepository;
import me.zhengjie.repository.TaskResultRepository;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
@RequiredArgsConstructor
public class WcsStatus {

    private final DeviceStatusRepository deviceStatusRepository;
    private final TaskResultRepository taskResultRepository;

    /**
     * 判断当前码垛机时候可以接收入库任务
     * @return true可以发送，false不能发送
     */
    public boolean allowSendTask(){
        try {
            List<DeviceStatus> deviceStatusList = deviceStatusRepository.findNewDeviceStatus();
            DeviceStatus deviceStatus = deviceStatusList.get(0);
            Integer status = deviceStatus.getStatus();
            return status == 1;
        } catch (Exception e) {
            log.error("没有查询到码垛机状态信息");
            return false;
        }
//        return status == 0 || status == 2;
    }

    /**
     * 判断当前码垛机时候可以接收入库任务
     * @return true可以发送，false不能发送
     */
    public boolean allowSendOutBoundMission(String code){
        Integer status = deviceStatusRepository.getStatusByCode(code);
        TaskResult taskResult = taskResultRepository.findOutBoundTask();
        // 设备状态=1是正常，并且没有执行中的出库任务
        return status ==1 && !ObjectUtil.isEmpty(taskResult);
    }

    /**
     * 判断码垛机取货完成与否
     * @param code 码垛机编码
     * @return true表示取货完成，false表示取货没有完成
     */
    public boolean canStartPlc(String code){
        return true;
    }
}
